package com.summba.analysis;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import org.apache.commons.dbutils.ResultSetHandler;

import com.summba.analysis.util.WeiboAccount;
import com.summba.mysql.dao.DaoBase;
import com.summba.mysql.dao.DataSourceFactory;

public class StatisticsAdsUrlClass extends DaoBase{
	
	public StatisticsAdsUrlClass() throws Exception{
		setDataSource(DataSourceFactory.getDataSource());
		Pattern p = Pattern.compile("http://[\\w\\.\\-/:]+",Pattern.CASE_INSENSITIVE);
		List<WeiboAccount> userList = this.getAllUserList();
		for (Iterator<WeiboAccount> iterator = userList.iterator(); iterator.hasNext();) {
			WeiboAccount weiboAccount = (WeiboAccount) iterator.next();
			List<String> stringList = getAllAdsContent(weiboAccount.getID());
			Map<String,Integer> urlMapper = new HashMap<String, Integer>();
			int urlCount = 0;
			for (String string : stringList) {
				//System.out.println("end index"+string.indexOf("&name=wb",0));
				//System.out.println("string: "+string);
				Matcher m = p.matcher(string);
				if(m.find()){
					if( urlMapper.containsKey(m.group()) ){
						urlMapper.put(m.group(), urlMapper.get(m.group())+1);
					}else{
						urlMapper.put(m.group(), 1);
					}
					urlCount++;
				}
			}
			if(urlMapper.isEmpty()){
				
			}else{
				System.out.println("=====================================");
				System.out.println("UserName: "+weiboAccount.getUserName());
				System.out.println("=====================================");
				this.showResult(urlMapper);
				System.out.println("=====================================");
				System.out.println("urlCount: "+urlCount);
				System.out.println("-------------------------------------");
				
			}
		}
		
	}
	
	private List<WeiboAccount> getAllUserList() throws Exception{
		
		String queryUserList = "select id,user_id,user_name from wb_user";
		ResultSetHandler< List<WeiboAccount> > queryUserListHandler = 
			new ResultSetHandler< List<WeiboAccount> >(){

				@Override
				public List<WeiboAccount> handle(ResultSet rs)
						throws SQLException {
					List<WeiboAccount> tempList = new ArrayList<WeiboAccount>();
					
					while(rs.next()) {
						WeiboAccount tempObj = new WeiboAccount();
						//System.out.println("rs.getInt(1): "+rs.getInt(1));
						//System.out.println("rs.getString(2): "+rs.getString(2));
						//System.out.println("rs.getString(3): "+rs.getString(3));
						tempObj.setID(rs.getInt(1));
						tempObj.setUserID(rs.getString(2));
						tempObj.setUserName(rs.getString(3));
						tempList.add(tempObj);
					}
					return tempList;
				}
			
			};
		
		
		List<WeiboAccount> tempList = findBean(queryUserList,queryUserListHandler);
		
		return tempList;
	}
	
	private List<String> getAllAdsContent(int user_id) throws Exception{
		String queryUserAllAdsContent = "select text from wb_advertisement_history where user_id = ?";
		ResultSetHandler < List<String> > queryUserAllAdsContentHandler =
			new ResultSetHandler< List<String> >(){

				@Override
				public List<String> handle(ResultSet rs) throws SQLException {
					List<String> tempList = new ArrayList<String>();
					while(rs.next()){
						//System.out.println("rs.getString(1): "+rs.getString(1));
						tempList.add(rs.getString(1));
					}
					return tempList;
				}
			
		};
		List<String> tempList = findBean(queryUserAllAdsContent,queryUserAllAdsContentHandler,user_id);
		return tempList;
	}
	
	public void showResult( Map<String, Integer> wordMapper){
		List<Map.Entry<String, Integer>> infoIds = 
			new ArrayList< Map.Entry<String, Integer> >(wordMapper.entrySet());
		
		Collections.sort(infoIds, 
				new Comparator<Map.Entry<String, Integer>>() { 
					public int compare(Map.Entry<String, Integer> o1, 
					Map.Entry<String, Integer> o2) { 
						return (o2.getValue() - o1.getValue()); 
					} 
				});
		for (int i = 0; i < infoIds.size(); i++) { 
			String id = infoIds.get(i).toString();
			//Common.appendFile(resultFile, id+ "\n");
			System.out.println(id); 
		}
	}
}
